package com.base.control;

import android.app.ActionBar;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by yjh on 15-3-10.
 */
public class ActionBarControl {
    /**
     * 为指定
     * @param act
     * @param layoutid
     * @return
     */
    public static View setActionBar(Activity act, int layoutid) {
        ActionBar actionBar = act.getActionBar();
        View actionBarView = null;

        if(actionBar != null) {
//            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
//            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

            //填充指定布局
            LayoutInflater f = LayoutInflater.from(act);
            actionBarView = f.inflate(layoutid, null);

            actionBar.setCustomView(actionBarView, new
                    ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
                    ActionBar.LayoutParams.MATCH_PARENT));
            actionBar.setDisplayShowHomeEnabled(true);

            View homeIcon;
            homeIcon = act.findViewById(android.R.id.home);
            //找到后获得他的父控件，然后隐藏即可
            if(homeIcon != null)
                ((View) homeIcon.getParent()).setVisibility(View.GONE);
        }

        return actionBarView;
    }

    public static View setHeaderBar(Activity act, ViewGroup root, int layoutid) {
        View actionBarView = null;

        //填充指定布局
        LayoutInflater f = LayoutInflater.from(act);
        actionBarView = f.inflate(layoutid, root);
//        root.addView(f.inflate(layoutid, null),
//                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        return actionBarView;
    }

}
